import ax from '@/utils/request'

// POST /processDef/add 新增
export function add(data) {
  return ax.request({
    url: 'processDef/add',
    method: 'post',
    data
  })
}
// POST /processDef/edit 编辑
export function edit(data) {
  return ax.request({
    url: 'processDef/edit',
    method: 'post',
    data
  })
}
// POST /processDef/list 流程基本信息
export function list(data) {
  return ax.request({
    url: 'processDef/list',
    method: 'post',
    data
  })
}
// POST /processDef/listByDefid 流程环节信息
export function listByDefid(params) {
  return ax.request({
    url: 'processDef/listByDefid',
    method: 'post',
    params
  })
}

export function getInstanceList(data) {
  return ax.request({
    url: 'flowInstance/getInstanceList',
    method: 'post',
    data
  })
}

export function print(data, fileName) {
  return new Promise((resolve, reject) => {
    ax.request({
      url: '/flowInstance/print',
      method: 'post',
      data,
      responseType: 'blob',
      timeout: 30 * 60000
    }).then(res => {
      const href = window.URL.createObjectURL(new Blob([res.data]))
      if (window.navigator.msSaveBlob) {
        const blob = new Blob([res], {
          type: 'application/json;charset=utf-8'
        })
        try {
          window.navigator.msSaveBlob(blob, fileName)
        } catch (e) {
  
        }
      } else {
        // 谷歌浏览器 创建a标签 添加download属性下载
        const downloadElement = document.createElement('a')
        downloadElement.href = href
        // downloadElement.target = '_blank';
        downloadElement.download = fileName + '.docx' // 下载后文件名
        document.body.appendChild(downloadElement)
        downloadElement.click() // 点击下载
        document.body.removeChild(downloadElement) // 下载完成移除元素
        window.URL.revokeObjectURL(href) // 释放掉blob对象
      }
      resolve( { code: 200 });
    }).catch(() => {
      reject( { code: 500 });
    })
  })
}
